import cv2

# 读取摄像头数据
cap = cv2.VideoCapture(0)

# 设置保存视频的参数
save_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
save_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter.fourcc(*'XVID')
out = cv2.VideoWriter('../images/output.avi', fourcc, 20.0, (save_width, save_height))
i = 0
while True:
    success, image = cap.read()
    cv2.imshow("IMG", image)
    # 保存每一帧为一张图片
    cv2.imwrite(f'../images/{i}.jpg', image)
    # 保存每一帧图像到视频文件
    out.write(image)
    # 等待1毫秒，检测键盘输入
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    i += 1

# 循环结束后释放摄像头资源和关闭窗口
cap.release()
out.release()
cv2.destroyAllWindows()
